package com.liyujie.dyeingmachine.utils;

public class MathUtil {


    /**
     * 对int数组的指定区间求最小值的下标
     */
    public static int getIntArrayMinIndexBetween(int[] src, int srcPos, int length) {
        int index = Math.max(srcPos, 0);
        int effectiveLength = Math.min(srcPos + length, src.length);

        for (int i = index; i < effectiveLength; i++) {
            if (src[index] > src[i]) {
                index = i;
            }
        }
        return index;
    }

    /**
     * 对int数组的指定区间求最大值的下标
     */
    public static int getIntArrayMaxIndexBetween(int[] src, int srcPos, int length) {
        int index = Math.max(srcPos, 0);
        int effectiveLength = Math.min(srcPos + length, src.length);

        for (int i = index; i < effectiveLength; i++) {
            if (src[index] < src[i]) {
                index = i;
            }
        }
        return index;
    }

    /**
     * 取指定索引间的平均值
     */
    public static int getAverage(int[] src, int srcPos, int length) {
        if (srcPos + length >= src.length || srcPos <= 0) {
            return -1;
        }
        return getSum(src, srcPos, length) / length;
    }

    /**
     * 取指定索引的和
     */
    public static int getSum(int[] src, int srcPos, int length) {
        if (srcPos + length >= src.length || srcPos <= 0) {
            return -1;
        }

        int sum = 0;
        int effectiveLength = Math.min(srcPos + length, src.length);
        for (int i = srcPos; i < effectiveLength; i++) {
            sum += src[i];
        }
        return sum;
    }

    /**
     * 2个对应数组，线性取值
     * 从小到大查pointArray
     */
    public static float getLinearValue(float[] pointArray, float[] valueArray, float point) {
        int index = 0;
        while (index < pointArray.length && point > pointArray[index]) {
            index++;
        }

        if (index == 0 && pointArray.length >= 2) {
            float temp = (valueArray[index + 1] - valueArray[index]) / (pointArray[index + 1] - pointArray[index]);
            return valueArray[index] - (pointArray[index] - point) * temp;
        } else if (index == pointArray.length && index >= 2) {
            float temp = (valueArray[index - 1] - valueArray[index - 2]) / (pointArray[index - 1] - pointArray[index - 2]);
            return (point - pointArray[index - 1]) * temp + valueArray[index - 1];
        } else {
            float temp = (valueArray[index] - valueArray[index - 1]) / (pointArray[index] - pointArray[index - 1]);
            return (point - pointArray[index - 1]) * temp + valueArray[index - 1];
        }
    }

    /**
     * 保留float 3位小数
     */
    public static float formatFloat(float data) {
        return Math.round(data * 1000) / 1000F;
    }

    /**
     * 左边拐点index计算
     */
    public static int getLeftIndex(int[] src, int srcIndex) {
        int count = 0;
        int index = srcIndex - 1;
        while (count < 5) {
            if (src[index - 1] - src[index] > 300) {
                count = 0;
            } else {
                count++;
            }
            index--;
        }
        return index;
    }

    /**
     * 从小到大排序
     */
    public static void sortl2h(float[] array) {
        for (int i = 1; i < array.length; i++) {
            float temp = array[i];
            int index = i;
            for (int j = i; j > 0; j--) {
                if (temp < array[j - 1]) {
                    array[j] = array[j - 1];
                } else {
                    index = j;
                    break;
                }
            }
            array[index] = temp;
        }
    }

    /**
     * 从大到小排序
     */
    public static void sorth2l(float[] array) {
        for (int i = 1; i < array.length; i++) {
            float temp = array[i];
            int index = i;
            for (int j = i; j > 0; j--) {
                if (temp > array[j - 1]) {
                    array[j] = array[j - 1];
                } else {
                    index = j;
                    break;
                }
            }
            array[index] = temp;
        }
    }
}
